<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        v-loading="loading"
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-tabs
              v-model="activeuuggdb"
              type="card"
              tab-position="top"
              class="mb-20"
            >
              <el-tab-pane label="阶段 1">
                <el-col :span="24">
                  <el-collapse
                    :accordion="false"
                    v-model="activewqmslv"
                    class="mb-20"
                  >
                    <el-collapse-item title="阶段信息" name="1">
                      <el-col :span="24">
                        <el-form-item label="说明" prop="description">
                          <el-input
                            v-model="dataForm.description"
                            placeholder="请输入"
                            :maxlength="500"
                            :style="{ width: '100%' }"
                            true
                            type="textarea"
                            :autosize="{ minRows: 4, maxRows: 4 }"
                          >
                          </el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="24">
                        <el-form-item label="阶段内闯关" prop="challenge">
                          <el-switch
                            v-model="dataForm.challenge"
                            :active-value="1"
                            :inactive-value="0"
                          >
                          </el-switch>
                        </el-form-item>
                      </el-col>
                      <el-col :span="24">
                        <el-form-item label="排序" prop="sortCode">
                          <el-input-number
                            v-model="dataForm.sortCode"
                            placeholder="数字文本"
                            :min="1"
                            :step="1"
                            controls-position="right"
                          >
                          </el-input-number>
                        </el-form-item>
                      </el-col>
                    </el-collapse-item>
                  </el-collapse>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="0">
                    <div class="jsbos-common-title">
                      <h2>学习内容</h2>
                    </div>
                    <el-table
                      :data="dataForm.temsSubjectStageRelationList"
                      size="mini"
                    >
                      <el-table-column
                        type="index"
                        width="50"
                        label="序号"
                        align="center"
                      />
                      <el-table-column prop="subjectId" label="专题ID">
                        <template slot-scope="scope">
                          <el-input
                            v-model="scope.row.subjectId"
                            placeholder="请输入"
                            clearable
                            :style="{ width: '100%' }"
                          >
                          </el-input>
                        </template>
                      </el-table-column>
                      <el-table-column prop="subjectStageId" label="阶段ID">
                        <template slot-scope="scope">
                          <el-input
                            v-model="scope.row.subjectStageId"
                            placeholder="请输入"
                            clearable
                            :style="{ width: '100%' }"
                          >
                          </el-input>
                        </template>
                      </el-table-column>
                      <el-table-column prop="objectType" label="对象类型">
                        <template slot-scope="scope">
                          <el-select
                            v-model="scope.row.objectType"
                            placeholder="请选择"
                            clearable
                            :style="{ width: '100%' }"
                          >
                            <el-option
                              v-for="(item, index) in objectTypeOptions"
                              :key="index"
                              :label="item.fullName"
                              :value="item.id"
                              :disabled="item.disabled"
                            ></el-option>
                          </el-select>
                        </template>
                      </el-table-column>
                      <el-table-column prop="objectId" label="对象主键">
                        <template slot-scope="scope">
                          <el-input
                            v-model="scope.row.objectId"
                            placeholder="请输入"
                            clearable
                            :style="{ width: '100%' }"
                          >
                          </el-input>
                        </template>
                      </el-table-column>
                      <el-table-column prop="type" label="类型">
                        <template slot-scope="scope">
                          <el-select
                            v-model="scope.row.type"
                            placeholder="请选择"
                            clearable
                            :style="{ width: '100%' }"
                          >
                            <el-option
                              v-for="(item, index) in typeOptions"
                              :key="index"
                              :label="item.fullName"
                              :value="item.id"
                              :disabled="item.disabled"
                            ></el-option>
                          </el-select>
                        </template>
                      </el-table-column>
                      <el-table-column prop="sortCode" label="排序">
                        <template slot-scope="scope">
                          <el-input-number
                            v-model="scope.row.sortCode"
                            placeholder="数字文本"
                            :style="{ width: '100%' }"
                            :step="1"
                            controls-position="right"
                          >
                          </el-input-number>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作" width="50">
                        <template slot-scope="scope">
                          <el-button
                            size="mini"
                            type="text"
                            class="jsbos-table-delBtn"
                            @click="
                              delTemsSubjectStageRelationList(scope.$index)
                            "
                          >
                            删除
                          </el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                    <div
                      class="table-actions"
                      @click="addTemsSubjectStageRelationList()"
                    >
                      <el-button type="text" icon="el-icon-plus"
                        >添加</el-button
                      >
                    </div>
                  </el-form-item>
                </el-col>
              </el-tab-pane>
              <el-tab-pane label="阶段 2"> </el-tab-pane>
            </el-tabs>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        subjectId: "",
        name: "学习内容",
        description: "",
        challenge: 0,
        sortCode: 1,
        temsSubjectStageRelationList: [],
      },
      activeuuggdb: "0",
      activewqmslv: ["1"],
      rules: {},

      objectTypeOptions: [
        { fullName: "选项一", id: "1" },
        { fullName: "选项二", id: "2" },
      ],
      typeOptions: [
        { fullName: "选修 ", id: "1" },
        { fullName: "必修", id: "2" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    temsSubjectStageRelationExist() {
      let isOk = true;
      for (
        let i = 0;
        i < this.dataForm.temsSubjectStageRelationList.length;
        i++
      ) {
        const e = this.dataForm.temsSubjectStageRelationList[i];
      }
      return isOk;
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsSubjectStage/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          if (!this.temsSubjectStageRelationExist()) return;
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsSubjectStage",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsSubjectStage/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    addTemsSubjectStageRelationList() {
      let item = {
        subjectId: undefined,
        subjectStageId: undefined,
        objectType: undefined,
        objectId: undefined,
        type: undefined,
        sortCode: undefined,
      };
      this.dataForm.temsSubjectStageRelationList.push(item);
    },
    delTemsSubjectStageRelationList(index) {
      this.dataForm.temsSubjectStageRelationList.splice(index, 1);
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.challenge = parseInt(_data.challenge);
      for (let i = 0; i < _data.temsSubjectStageRelationList.length; i++) {
        var _list = _data.temsSubjectStageRelationList[i];
      }
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.challenge = parseInt(_dataAll.challenge);
      for (let i = 0; i < _dataAll.temsSubjectStageRelationList.length; i++) {
        var _list = _dataAll.temsSubjectStageRelationList[i];
      }
      this.dataForm = _dataAll;
    },
  },
};
</script>
